﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using General.Entities;
using Microsoft.EntityFrameworkCore;
using General.Services.Category;
using General.Core;
using General.Core.Extensions;
using General.Core.Data;
using General.Services.Setting;
using General.Core.Librs;
using General.Framework;
using General.Framework.Infrastructure;
using General.Framework.Security.Admin;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.Extensions.Caching.Memory;
using System.IO;
using General.Framework.Menu;
using General.Core.Infrastructure;
using Microsoft.AspNetCore.HttpOverrides;
using General.Framework.Middleware;
using General.Framework.Filters;
using General.Framework.Options;
using General.Services.Install;

namespace General.Mvc
{
    public class Startup
    {

        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public IServiceProvider ConfigureServices(IServiceCollection services)
        { 
            //
            var engine = EngineContext.Create(new GeneralEngine(Configuration));

            engine.Initialize(services);
            var serviceProvider = engine.ConfigureServices(services);

            return serviceProvider;
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, IInstallService installService)
        {
            installService.Install();

            app.UseStaticFiles();

            app.UseAuthentication();
            app.UseDeveloperExceptionPage();
            //
            //app.UseSession();
            app.UseForwardedHeaders(new ForwardedHeadersOptions
            {
                ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
            });

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                  name: "areas",
                  template: "{area:exists}/{controller=Login}/{action=Index}/{id?}"
                );
            });
        }
    }
}
